<div id="content">
    <h3><a href="?p=home&t=student">Geoplaza</a><a href="?p=home&t=student"> &rsaquo;&rsaquo; studenten</a> &rsaquo;&rsaquo; Mijn profiel bewerken</h3>
    <div id="Profilehead">Profiel bewerken</div>
    <div id="profile">
        <?php
        require_once(CONFIG_PATH.'appvars.php');
        require_once(CONFIG_PATH.'config.php');

        // Make sure the user is logged in before going any further.
        if (!isset($_SESSION['user_id'])) {
            echo '<p class="login">Please <a href="?p=home&t=student">log in</a> to access this page.</p>';
            exit();
        }

        // Connect to the database
        $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_DATABASE);

        if (isset($_POST['submit'])) {
            // Grab the profile data from the POST
            $voornaam = mysqli_real_escape_string($dbc, trim($_POST['voornaam']));
            $achternaam = mysqli_real_escape_string($dbc, trim($_POST['achternaam']));
            $geslacht = mysqli_real_escape_string($dbc, trim($_POST['geslacht']));
            $geboortedatum = mysqli_real_escape_string($dbc, trim($_POST['birthdate']));
            $adres = mysqli_real_escape_string($dbc, trim($_POST['adres']));
            $postcode = mysqli_real_escape_string($dbc, trim($_POST['postcode']));
            $woonplaats = mysqli_real_escape_string($dbc, trim($_POST['woonplaats']));
            $province = mysqli_real_escape_string($dbc, trim($_POST['province']));
            $telefoon = mysqli_real_escape_string($dbc, trim($_POST['telefoon']));
            $biography = mysqli_real_escape_string($dbc, trim($_POST['biography']));
            $opleiding = mysqli_real_escape_string($dbc, trim($_POST['opleiding']));
            $niveau = mysqli_real_escape_string($dbc, trim($_POST['niveau']));
            $school = mysqli_real_escape_string($dbc, trim($_POST['school']));
            $stagedatum = mysqli_real_escape_string($dbc, trim($_POST['stagedatum']));
            $email = mysqli_real_escape_string($dbc, trim($_POST['email']));
            $old_picture = mysqli_real_escape_string($dbc, trim($_POST['old_picture']));
            $new_picture = mysqli_real_escape_string($dbc, trim($_FILES['new_picture']['name']));
            $new_picture_type = $_FILES['new_picture']['type'];
            $new_picture_size = $_FILES['new_picture']['size'];
            
            list($new_picture_width, $new_picture_height) = getimagesize($_FILES['new_picture']['tmp_name']);
            $error = false;
            
            // Validate and move the uploaded picture file, if necessary
            if (!empty($new_picture)) {
                if ((($new_picture_type == 'image/gif') || ($new_picture_type == 'image/jpeg') || ($new_picture_type == 'image/pjpeg') ||
                        ($new_picture_type == 'image/png')) && ($new_picture_size > 0) && ($new_picture_size <= MM_MAXFILESIZE) &&
                        ($new_picture_width <= MM_MAXIMGWIDTH) && ($new_picture_height <= MM_MAXIMGHEIGHT)) {
                    if ($_FILES['new_picture']['error'] == 0) {
                        // Move the file to the target upload folder
                        $target = MM_UPLOADPATH . basename($new_picture);
                        if (move_uploaded_file($_FILES['new_picture']['tmp_name'], $target)) {
                            // The new picture file move was successful, now make sure any old picture is deleted
                            if (!empty($old_picture) && ($old_picture != $new_picture)) {
                                @unlink(MM_UPLOADPATH . $old_picture);
                            }
                        } else {
                            // The new picture file move failed, so delete the temporary file and set the error flag
                            @unlink($_FILES['new_picture']['tmp_name']);
                            $error = true;
                            echo '<p class="error">Sorry, there was a problem uploading your picture.</p>';
                        }
                    }
                } else {
                    // The new picture file is not valid, so delete the temporary file and set the error flag
                    @unlink($_FILES['new_picture']['tmp_name']);
                    $error = true;
                    echo '<p class="error">Your picture must be a GIF, JPEG, or PNG image file no greater than ' . (MM_MAXFILESIZE / 1024) .
                    ' KB and ' . MM_MAXIMGWIDTH . 'x' . MM_MAXIMGHEIGHT . ' pixels in size.</p>';
                }
            }

            // Update the profile data in the database
            if (!$error) {
                if (!empty($voornaam) && !empty($achternaam) && !empty($geslacht) && !empty($geboortedatum) && !empty($woonplaats) && !empty($province)) {

                    // Only set the picture column if there is a new picture
                    if (!empty($new_picture)) {
                        $query = "UPDATE studentprofiel SET voornaam = '$voornaam', achternaam = '$achternaam', geslacht = '$geslacht', " .
                                " geboortedatum = '$geboortedatum', adres = '$adres', postcode = '$postcode', woonplaats = '$woonplaats', provencie = '$province', telefoon = '$telefoon', biography = '$biography', foto = '$new_picture', opleiding = '$opleiding', niveau = '$niveau', school = '$school', stagedate = '$stagedatum', email = '$email' WHERE studentID = '" . $_SESSION['user_id'] . "'";
                    } else {
                        $query = "UPDATE studentprofiel SET voornaam = '$voornaam', achternaam = '$achternaam', geslacht = '$geslacht', " .
                                " geboortedatum = '$geboortedatum', adres = '$adres', postcode = '$postcode', woonplaats = '$woonplaats', provencie = '$province', telefoon = '$telefoon', biography = '$biography', opleiding = '$opleiding', niveau = '$niveau', school = '$school', stagedate = '$stagedatum', email = '$email' WHERE studentID = '" . $_SESSION['user_id'] . "'";
                    }
                    mysqli_query($dbc, $query);

                    //$home_url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . '/index.php?p=viewprofile';
                    header('Location: ' . BASE_URL . '?p=viewprofile&t=student');

                    mysqli_close($dbc);
                    exit();
                } else {
                    echo '<p class="error">You must enter all of the profile data (the picture is optional).</p>';
                }
            }
        } // End of check for form submission
        else {
            // Grab the profile data from the database
            $query = "SELECT voornaam, achternaam, geslacht, geboortedatum, adres, postcode, woonplaats, provencie, telefoon, biography, foto, opleiding, niveau, school, stagedate, email FROM studentprofiel WHERE studentID = '" . $_SESSION['user_id'] . "'";
            $data = mysqli_query($dbc, $query);
            $row = mysqli_fetch_array($data);

            if ($row != NULL) {
                $voornaam = $row['voornaam'];
                $achternaam = $row['achternaam'];
                $geslacht = $row['geslacht'];
                $geboortedatum = $row['geboortedatum'];
                $adres = $row['adres'];
                $postcode = $row['postcode'];
                $woonplaats = $row['woonplaats'];
                $province = $row['provencie'];
                $telefoon = $row['telefoon'];
                $biography = $row['biography'];
                $opleiding = $row['opleiding'];
                $niveau = $row['niveau'];
                $school = $row['school'];
                $stagedatum = $row['stagedate'];
                $email = $row['email'];
            } else {
                echo '<p class="error">There was a problem accessing your profile.</p>';
            }
        }
        mysqli_close($dbc);
        ?>
        <form enctype="multipart/form-data" method="post" action="?p=editprofile&t=student">
            <input type="hidden" name="MAX_FILE_SIZE" value="<?php echo MM_MAXFILESIZE; ?>" />
            <fieldset class="info">
                <legend>Persoonlijke informatie</legend>
                <p class="titel"> Voornaam</p>
                <input name="voornaam" id="voornaam" class="edit" type="text" value="<?php if (!empty($voornaam))
            echo $voornaam; ?>"/>

                <p class="titel">Achternaam</p>
                <input name="achternaam" id="achternaam" class="edit" type="text" value="<?php if (!empty($achternaam))
                           echo $achternaam; ?>"/>

                <p class="titel">Ik ben een</p>
                <select name="geslacht" class="edit">
                    <option value="man" <?php if (!empty($geslacht) && $geslacht == 'man')
                           echo 'selected = "selected"'; ?>>Man</option>
                    <option value="vrouw" <?php if (!empty($geslacht) && $geslacht == 'vrouw')
                                echo 'selected = "selected"'; ?>>Vrouw</option>
                </select>

                <p class="titel">Birthdate:</p>
                <input type="text" id="birthdate" class="edit" name="birthdate" value="<?php
                            if (!empty($geboortedatum))
                                echo $geboortedatum; else
                                echo 'YYYY-MM-DD';
        ?>" />
                     <p class="titel">Adres</p>
                     <input name="adres" id="adres" class="edit" type="text" value="<?php if (!empty($adres))
                                echo $adres; ?>"/>

                     <p class="titel">Postcode</p>
                     <input name="postcode" id="postcode" class="edit" type="text" value="<?php if (!empty($postcode))
                                echo $postcode; ?>"/>

                     <p class="titel">Woonplaats</p>
                     <input name="woonplaats" id="woonplaats" class="edit" type="text"value="<?php if (!empty($woonplaats))
                                echo $woonplaats; ?>" />

                     <p class="titel">Provincie</p>
                     <select name="province" class="edit">
                         <option value="Groningen" <?php if (!empty($province) && $province == 'Groningen')
                                echo 'selected = "selected"'; ?>>Groningen</option>
                    <option value="Friesland" <?php if (!empty($province) && $province == 'Friesland')
                                echo 'selected = "selected"'; ?>>Friesland</option>
                    <option value="Drenthe" <?php if (!empty($province) && $province == 'Drenthe')
                                echo 'selected = "selected"'; ?>>Drenthe</option>
                    <option value="Overijssel" <?php if (!empty($province) && $province == 'Overijssel')
                                echo 'selected = "selected"'; ?>>Overijssel</option>
                    <option value="Flevoland" <?php if (!empty($province) && $province == 'Flevoland')
                                echo 'selected = "selected"'; ?>>Flevoland</option>
                    <option value="Gelderland" <?php if (!empty($province) && $province == 'Gelderland')
                                echo 'selected = "selected"'; ?>>Gelderland</option>
                    <option value="Utrecht" <?php if (!empty($province) && $province == 'Utrecht')
                                echo 'selected = "selected"'; ?> >Utrecht</option>
                    <option value="Noord-Holland" <?php if (!empty($province) && $province == 'Noord-Holland')
                                echo 'selected = "selected"'; ?>>Noord-Holland</option>
                    <option value="Zuid-Holland" <?php if (!empty($province) && $province == 'Zuid-Holland')
                                echo 'selected = "selected"'; ?>>Zuid-Holland</option>
                    <option value="Zeeland" <?php if (!empty($province) && $province == 'Zeeland')
                                echo 'selected = "selected"'; ?>>Zeeland</option>
                    <option value="Noord-Barabant" <?php if (!empty($province) && $province == 'Noord-Barabant')
                                echo 'selected = "selected"'; ?>>Noord-Barabant</option>
                    <option value="Limburg" <?php if (!empty($province) && $province == 'Limburg')
                                echo 'selected = "selected"'; ?>>Limburg</option>
                </select>

                <p class="titel">Telefoon</p>
                <input name="telefoon" id="telefoon" class="edit" type="text" value="<?php if (!empty($telefoon))
                                echo $telefoon; ?>" />
                     <p class="titel">Gebruikersnaam (je email)</p>
                     <input name="email" id="email" type="text" class="edit" value="<?php if (!empty($email))
                                echo $email; ?>"/>

                     <input type="hidden" name="old_picture" class="edit" value="<?php if (!empty($old_picture))
                                echo $old_picture; ?>" />

                     <p class="titel">Picture:</p>
                     <input type="file" id="new_picture" class="edit" name="new_picture" />
                <?php
                            if (!empty($old_picture)) {
                                echo '<img class="profile" src="' . MM_UPLOADPATH . $old_picture . '" alt="Profile Picture" />';
                            }
                ?>
                            <p class="titel">Huidige opleiding </p>
                            <input name="opleiding" id="opleiding" class="edit" type="text" value="<?php if (!empty($opleiding))
                                echo $opleiding; ?>" />

                     <p class="titel">Naam school</p>
                     <input name="school" id="school" class="edit" type="text" value="<?php if (!empty($school))
                                echo $school; ?>" />

                     <p class="titel">Stagedatum:</p>
                     <input type="text" id="stagedatum" class="edit" name="stagedatum" value="<?php
                            if (!empty($stagedatum))
                                echo $stagedatum; else
                                echo 'YYYY-MM-DD';
                ?>" />

                     <p class="titel">Niveau</p>
                     <ul  class="edit">
                         <li><input type="radio" name="niveau" value="MBO" <?php if (!empty($niveau) && $niveau == 'MBO')
                                echo 'checked'; ?>>MBO</li>
                    <li><input type="radio" name="niveau" value="HBO" <?php if (!empty($niveau) && $niveau == 'HBO')
                                   echo 'checked'; ?>>HBO</li>
                    <li><input type="radio" name="niveau" value="WO" <?php if (!empty($niveau) && $niveau == 'WO')
                                   echo 'checked'; ?>>WO</li>
                </ul>
                <p class="titel">Biography</p>
                <textarea cols="" rows="" class="edit"  name="biography" id="biography"></textarea><br/>
            </fieldset>
            <input type="submit" value="Save Profile" name="submit" />
        </form>
    </div>
</div>